/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.clientpackets;

import jmmos.gameserver.datatables.GuildTable;
import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.MMOGuildMember;
import jmmos.gameserver.model.MMOWorld;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.network.SystemMessageId;
import jmmos.gameserver.network.serverpackets.ActionFailed;
import jmmos.gameserver.taskmanager.AttackStanceTaskManager;

import gnu.trove.procedure.TObjectProcedure;

public final class RequestStopPledgeWar extends MMOGameClientPacket
{
	private static final String _C__05_REQUESTSTOPPLEDGEWAR = "[C] 05 RequestStopPledgeWar";
	
	private String _pledgeName;
	
	@Override
	protected void readImpl()
	{
		_pledgeName = readS();
	}
	
	@Override
	protected void runImpl()
	{
		MMOPcInstance player = getClient().getActiveChar();
		if (player == null)
		{
			return;
		}
		MMOGuild playerGuild = player.getGuild();
		if (playerGuild == null)
		{
			return;
		}
		
		MMOGuild guild = GuildTable.getInstance().getGuildByName(_pledgeName);
		
		if (guild == null)
		{
			player.sendMessage("No such guild.");
			player.sendPacket(ActionFailed.STATIC_PACKET);
			return;
		}
		
		if (!playerGuild.isAtWarWith(guild.getGuildId()))
		{
			player.sendMessage("You aren't at war with this guild.");
			player.sendPacket(ActionFailed.STATIC_PACKET);
			return;
		}
		
		// Check if player who does the request has the correct rights to do it
		if ((player.getGuildPrivileges() & MMOGuild.CP_CL_PLEDGE_WAR) != MMOGuild.CP_CL_PLEDGE_WAR)
		{
			player.sendPacket(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT);
			return;
		}
		
		// _log.info("RequestStopPledgeWar: By leader or authorized player: " + playerGuild.getLeaderName() + " of guild: "
		// + playerGuild.getName() + " to guild: " + _pledgeName);
		
		// MMOPcInstance leader = MMOWorld.getInstance().getPlayer(guild.getLeaderName());
		// if(leader != null && leader.isOnline() == 0)
		// {
		// player.sendMessage("Guild leader isn't online.");
		// player.sendPacket(ActionFailed.STATIC_PACKET);
		// return;
		// }
		
		// if (leader.isProcessingRequest())
		// {
		// SystemMessage sm = SystemMessage.getSystemMessage(SystemMessage.S1_IS_BUSY_TRY_LATER);
		// sm.addString(leader.getName());
		// player.sendPacket(sm);
		// return;
		// }
		
		for (MMOGuildMember member : playerGuild.getMembers())
		{
			if ((member == null) || (member.getPlayerInstance() == null))
			{
				continue;
			}
			if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(member.getPlayerInstance()))
			{
				player.sendPacket(SystemMessageId.CANT_STOP_GUILD_WAR_WHILE_IN_COMBAT);
				return;
			}
		}
		
		GuildTable.getInstance().deleteguildswars(playerGuild.getGuildId(), guild.getGuildId());
		MMOWorld.getInstance().forEachPlayer(new ForEachPlayerBroadcastUserInfo(guild, player));
	}
	
	private final class ForEachPlayerBroadcastUserInfo implements TObjectProcedure<MMOPcInstance>
	{
		private final MMOPcInstance _player;
		private final MMOGuild _cln;
		
		protected ForEachPlayerBroadcastUserInfo(MMOGuild guild, MMOPcInstance player)
		{
			_cln = guild;
			_player = player;
		}
		
		@Override
		public final boolean execute(final MMOPcInstance cha)
		{
			if ((cha.getGuild() == _player.getGuild()) || (cha.getGuild() == _cln))
			{
				cha.broadcastUserInfo();
			}
			return true;
		}
	}
	
	@Override
	public String getType()
	{
		return _C__05_REQUESTSTOPPLEDGEWAR;
	}
}